/****************************************************************************
**
** Copyright (C) 2017 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the examples of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** BSD License Usage
** Alternatively, you may use this file under the terms of the BSD license
** as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
**   * Redistributions of source code must retain the above copyright
**     notice, this list of conditions and the following disclaimer.
**   * Redistributions in binary form must reproduce the above copyright
**     notice, this list of conditions and the following disclaimer in
**     the documentation and/or other materials provided with the
**     distribution.
**   * Neither the name of The Qt Company Ltd nor the names of its
**     contributors may be used to endorse or promote products derived
**     from this software without specific prior written permission.
**
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
**
** $QT_END_LICENSE$
**
****************************************************************************/
//import QtQuick 2.0
//import QtQuick.Controls 2.0
////import QtQuick.Layouts 1.3
//import QtQuick.Window 2.10
//import QtQuick.VirtualKeyboard 2.3
//import QtQuick.Dialogs 1.2
//import an.qt.mySerialPortHelper 1.0

import QtQuick 2.9
import QtQuick.Layouts 1.3
import QtQuick.Controls 2.3
import QtQuick.Controls.Material 2.1
import QtQuick.Controls.Universal 2.1
import Qt.labs.settings 1.0
import QtQuick.VirtualKeyboard 2.3

ApplicationWindow {
    id: window
    width: 800
    height: 800
    visible: true
    title: "serialQuick Demo V1.0"

    Settings {
        id: settings
        property string style: "Default"
    }

        InputPanel {
            id: inputPanel
            z: 99
            x: 0
            y: window.height
            width: window.width

            states: State {
                name: "visible"
                when: inputPanel.active
                PropertyChanges {
                    target: inputPanel
                    y: window.height - inputPanel.height
                }
            }
            transitions: Transition {
                from: ""
                to: "visible"
                reversible: true
                ParallelAnimation {
                    NumberAnimation {
                        properties: "y"
                        duration: 250
                        easing.type: Easing.InOutQuad
                    }
                }
            }
        }

    Shortcut {
        sequences: ["Esc", "Back"]
        enabled: stackView.depth > 1
        onActivated: {
            stackView.pop()
            listView.currentIndex = -1
        }
    }

    Shortcut {
        sequence: "Menu"
        onActivated: optionsMenu.open()
    }

    header: ToolBar {
        Material.foreground: "white"

        RowLayout {
            spacing: 20
            anchors.fill: parent

            ToolButton {
                icon.name: stackView.depth > 1 ? "back" : "drawer"
                onClicked: {
                    if (stackView.depth > 1) {
                        stackView.pop()
                        listView.currentIndex = -1
                    } else {
                        drawer.open()
                    }
                }
            }

            Label {
                id: titleLabel
                text: listView.currentItem ? listView.currentItem.text : "HJ212-MX Demo V1.0"
                font.pixelSize: 20
                elide: Label.ElideRight
                horizontalAlignment: Qt.AlignHCenter
                verticalAlignment: Qt.AlignVCenter
                Layout.fillWidth: true
            }

            ToolButton {
                icon.name: "menu"
                onClicked: optionsMenu.open()

                Menu {
                    id: optionsMenu
                    x: parent.width - width
                    transformOrigin: Menu.TopRight

                    MenuItem {
                        text: "Settings"
                        onTriggered: settingsDialog.open()
                    }
                    MenuItem {
                        text: "关于"
                        onTriggered: aboutDialog.open()
                    }
                }
            }
        }
    }

    Drawer {
        id: drawer
        width: Math.min(window.width, window.height) / 3 * 2
        height: window.height
        interactive: stackView.depth === 1

        ListView {
            id: listView

            focus: true
            currentIndex: -1
            anchors.fill: parent

            delegate: ItemDelegate {
                font.pixelSize: 25;
                width: parent.width
                text: model.title
                highlighted: ListView.isCurrentItem
                onClicked: {
                    listView.currentIndex = index
                    stackView.push(model.source)
                    drawer.close()
                }
            }

            model: ListModel {
                ListElement { title: "基本功能演示"; source: "qrc:/serialQuickDemo/pages/test.qml" }
//                ListElement { title: "站点参数设置"; source: "qrc:/serialQuickDemo/pages/setting.qml" }
//                ListElement { title: "BusyIndicator"; source: "qrc:/serialQuickDemo/pages/BusyIndicatorPage.qml" }
//                ListElement { title: "Button"; source: "qrc:/serialQuickDemo/pages/ButtonPage.qml" }
//                ListElement { title: "CheckBox"; source: "qrc:/serialQuickDemo/pages/CheckBoxPage.qml" }
//                ListElement { title: "ComboBox"; source: "qrc:/serialQuickDemo/pages/ComboBoxPage.qml" }
//                ListElement { title: "DelayButton"; source: "qrc:/serialQuickDemo/pages/DelayButtonPage.qml" }
//                ListElement { title: "Dial"; source: "qrc:/serialQuickDemo/pages/DialPage.qml" }
//                ListElement { title: "Dialog"; source: "qrc:/serialQuickDemo/pages/DialogPage.qml" }
//                ListElement { title: "Delegates"; source: "qrc:/serialQuickDemo/pages/DelegatePage.qml" }
//                ListElement { title: "Frame"; source: "qrc:/serialQuickDemo/pages/FramePage.qml" }
//                ListElement { title: "GroupBox"; source: "qrc:/serialQuickDemo/pages/GroupBoxPage.qml" }
//                ListElement { title: "PageIndicator"; source: "qrc:/serialQuickDemo/pages/PageIndicatorPage.qml" }
//                ListElement { title: "ProgressBar"; source: "qrc:/serialQuickDemo/pages/ProgressBarPage.qml" }
//                ListElement { title: "RadioButton"; source: "qrc:/serialQuickDemo/pages/RadioButtonPage.qml" }
//                ListElement { title: "RangeSlider"; source: "qrc:/serialQuickDemo/pages/RangeSliderPage.qml" }
//                ListElement { title: "ScrollBar"; source: "qrc:/serialQuickDemo/pages/ScrollBarPage.qml" }
//                ListElement { title: "ScrollIndicator"; source: "qrc:/serialQuickDemo/pages/ScrollIndicatorPage.qml" }
//                ListElement { title: "Slider"; source: "qrc:/serialQuickDemo/pages/SliderPage.qml" }
//                ListElement { title: "SpinBox"; source: "qrc:/serialQuickDemo/pages/SpinBoxPage.qml" }
//                ListElement { title: "StackView"; source: "qrc:/serialQuickDemo/pages/StackViewPage.qml" }
//                ListElement { title: "SwipeView"; source: "qrc:/serialQuickDemo/pages/SwipeViewPage.qml" }
//                ListElement { title: "Switch"; source: "qrc:/serialQuickDemo/pages/SwitchPage.qml" }
//                ListElement { title: "TabBar"; source: "qrc:/serialQuickDemo/pages/TabBarPage.qml" }
//                ListElement { title: "TextArea"; source: "qrc:/serialQuickDemo/pages/TextAreaPage.qml" }
//                ListElement { title: "TextField"; source: "qrc:/serialQuickDemo/pages/TextFieldPage.qml" }
//                ListElement { title: "ToolTip"; source: "qrc:/serialQuickDemo/pages/ToolTipPage.qml" }
//                ListElement { title: "Tumbler"; source: "qrc:/serialQuickDemo/pages/TumblerPage.qml" }
            }

            ScrollIndicator.vertical: ScrollIndicator { }
        }
    }

    StackView {
        id: stackView
        anchors.fill: parent

        initialItem: Pane {
            id: pane

            Text {
                color: "#0c00d5"
                font.pixelSize: 25;
                text: "当前版本:"+station.SoftCurrVer+"\r\n\r\n当前为演示版本,仅供测试验证使用,\r\n\r\n请点击左上角菜单按钮选择相应功能开始测试"
                //anchors.margins: 20
                //anchors.top: logo.bottom
                anchors.left: parent.left
                anchors.right: parent.right
                anchors.centerIn: parent
                //anchors.bottom: parent.bottom
//                horizontalAlignment: Label.AlignHCenter
//                verticalAlignment: Label.AlignVCenter
//                wrapMode: Label.Wrap
            }

        }
    }

    Dialog {
        id: settingsDialog;
        x: Math.round((window.width - width) / 2);
        y: Math.round(window.height / 6);
        width: Math.round(Math.min(window.width, window.height) / 3 * 2);
        modal: true;
        focus: true;
        title: "Settings";

        standardButtons: Dialog.Ok | Dialog.Cancel
        onAccepted: {
            settings.style = styleBox.displayText
            settingsDialog.close()
        }
        onRejected: {
            styleBox.currentIndex = styleBox.styleIndex
            settingsDialog.close()
        }

        contentItem: ColumnLayout {
            id: settingsColumn
            spacing: 20

            RowLayout {
                spacing: 10

                Label {
                    text: "Style:"
                }

                ComboBox {
                    id: styleBox
                    property int styleIndex: -1
                    model: availableStyles
                    Component.onCompleted: {
                        styleIndex = find(settings.style, Qt.MatchFixedString)
                        if (styleIndex !== -1)
                            currentIndex = styleIndex
                    }
                    Layout.fillWidth: true
                }
            }

            Label {
                text: "Restart required"
                color: "#e41e25"
                opacity: styleBox.currentIndex !== styleBox.styleIndex ? 1.0 : 0.0
                horizontalAlignment: Label.AlignHCenter
                verticalAlignment: Label.AlignVCenter
                Layout.fillWidth: true
                Layout.fillHeight: true
            }
        }
    }

    Dialog {
        id: aboutDialog
        modal: true;
        focus: true;
        title: "关于本系统"
        x: (window.width - width) / 2;
        y: window.height / 6;
        width: Math.min(window.width, window.height) / 3 * 2;
        contentHeight: aboutColumn.height;
        font.pixelSize: 18
        Column {
            id: aboutColumn;
            spacing: 20;

            Label {
                width: aboutDialog.availableWidth;
                text: "serialQuick Demo V1.0\r\n\r\nBased on Qt 5.10.0\r\n\r\nBuilt on "+station.SoftBuildOnTime+"\r\n\r\nCopyright 2018-2019 Moolink.Ltd. All rights reserved."

                wrapMode: Label.Wrap
                font.pixelSize: 12
            }

            Label {
                color:"blue"
                width: aboutDialog.availableWidth
                text: "-----------------------------------------------------------\r\n嵌入式硬件问题支持:\r\n\r\n        嘉兴安泰电子有限公司\r\n\r\n            移动电话:13957339007(沈先生)\r\n\r\n-----------------------------------------------------------\r\n系统软件问题支持:\r\n\r\n        嘉兴慕链信息技术有限公司\r\n\r\n            移动电话:13615737172(王先生)\r\n\r\n"
//                text: "技术支持:\r\n\r\n上海墨溪环保科技有限公司 (产品及系统服务支持)\r\n  嘉兴分公司:0573-\r\n\r\n嘉兴安泰电子有限公司 (嵌入式硬件问题支持)\r\n  固定电话:0573-\r\n  移动电话:13957339007(沈先生)\r\n\r\n嘉兴慕链信息技术有限公司 (系统软件问题支持)\r\n  固定电话:0573-87297521\r\n  移动电话:13615737172(王先生)\r\n\r\n"
                wrapMode: Label.Wrap
                font.pixelSize: 12
            }
        }
    }
    Dialog {


        id: updateDialog
        x: Math.round((window.width - width) / 2)
        y: Math.round(window.height / 6)
        width: Math.round(Math.min(window.width, window.height) / 3 * 2)
        modal: true
        focus: true
        title: "发现更新版本"
        font.pixelSize: 18

        standardButtons: Dialog.Yes | Dialog.Cancel
        onAccepted: {
            qmlconncpp.goUpdate();
            updateDialog.close();
        }
        onRejected: {
            station.NeedUpdate=false;
            updateDialog.close();
        }

        contentItem: ColumnLayout {
            id: updateColumn
            spacing: 20

            Label {
                text: "本地版本:"+station.SoftCurrVer+"\r\n\r\n线上版本:"+station.SoftOnlineVer+"\r\n\r\n是否要切换为线上版本?";
                font.pixelSize: 12
                color: "#e41e25"
            }
        }
        visible: station.NeedUpdate;
    }

}
